﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NicoView.Model.Service;
using System.Windows.Input;
using System.ComponentModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using NicoView;


using NicoView.Model.Service.Events;
using NicoView.Model.Service.Results;
using GalaSoft.MvvmLight.Command;
using NicoView.ViewModel.Message;


namespace NicoView.ViewModel
{
    public sealed class NicoViewAuthSetViewModel : NicoViewViewModelBase
    {
        public NicoPlaneUserAuth Auth { get; private set; }

        public delegate void AfterRegistration(IResult rslt);

        public event NicoAuthSet OnSet;



        public NicoViewAuthSetViewModel()
        {
            Auth = new NicoPlaneUserAuth();
            saveCommand_ = null;

            OnSet = null;
        }

        public string Mail
        {
            get
            {
                return Auth.Mail;
            }

            set
            {
                if (this.Auth.Mail != value)
                {
                    this.Auth.Mail = value;
                }
                this.RaisePropertyChanged(() => this.Mail);
            }
        }

        public string Password
        {
            get
            {
                return Auth.PassWord;
            }

            set
            {
                if (this.Auth.Mail != value)
                {
                    this.Auth.PassWord = value;
                }

                this.RaisePropertyChanged(() => this.Password);
            }
        }


        private ICommand saveCommand_;

        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand_ == null)
                {
                    saveCommand_ = new RelayCommand(() =>
                    {
                        Messenger.Default.Send(new GenericMessage<NicoViewAuthSetResultMessage>(this.Save()));
                    });

                }

                return saveCommand_;
            }
        }

        public NicoViewAuthSetResultMessage Save()
        {
            IResult checkResult = CanExecute(null);
            if (checkResult.Succeed)
            {
                if (OnSet != null) OnSet(new NicoAuthSetEventArgs { Auth = this.Auth });
                return new NicoViewAuthSetResultMessage(new Result());

            }
            return new NicoViewAuthSetResultMessage(checkResult);
            
        }


        public IResult CanExecute(object parameter)
        {
            IResult mailRslt = NotificationNicoAuth.CheckMail(Auth.Mail);
            IResult passRslt = NotificationNicoAuth.CheckPassword(Auth.PassWord);
            if (mailRslt.Succeed && passRslt.Succeed) return new Result();
            return new Result(mailRslt.Message + "\r\n" + passRslt.Message);
        }

    }
}
